package promote.brozen.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @author Brozen
 * @date 2020/9/30 4:30 PM
 * @email brozen@qq.com
 */
public class BufRef {


    public static void main(String[] args) {
        ByteBuf buf = Unpooled.buffer();

        buf.writeBytes(new byte[]{1, 2, 3, 4, 5});
        printRefCount(buf);

        // 引用+1
        buf.retain();
        printRefCount(buf);

        // 引用-1
        buf.release();
        printRefCount(buf);

        // 引用再-1，此时引用为0，会触发回收
        buf.release();
        printRefCount(buf);

    }

    private static void printRefCount(ByteBuf buf) {
        System.out.println("引用：" + buf.refCnt());
    }


}
